#include "Texture.h"
#include "TGA.h"

char * getPixel(char * buff, int h, int w, int bpp, int n, int m)
{
	char * res = new char[w * h * bpp / 8];
	int cnt = 0;
	for (int i = (n - 1) * h; i < n * h  ; i++)
		for (int j = (m - 1) * w * bpp / 8; j < m * w * bpp / 8; j++)
		{
			res[cnt++] = buff[i * 3 * w * 4 + j];
		}
	return res;
}

void Texture::SetID(const char* ID)
{
	strcpy(this->ID, ID);
}

char* Texture::GetID()
{
	return ID;
}

GLuint Texture::LoadTexture(const char* ID, const char* file, bool isTexture2D, GLuint filter)
{
	strcpy(this->ID, ID);
	if (!isTexture2D)
	{
		// Generate a texture object
		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

			int width, height, bpp;
			char * bufferTGA = LoadTGA(file, &width, &height, &bpp );
			// load the image data into OpenGL ES texture resource

			int widthSmall = width / 4;
			int heightSmall = height / 3;
			//589828
			char* sBuffer[6];
			// Load the cube face - Positive X
			sBuffer[0] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,2,3);
			sBuffer[1] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,2,1);
			sBuffer[2] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,1,2);
			sBuffer[3] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,3,2);
			sBuffer[4] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,2,2);
			sBuffer[5] = getPixel(bufferTGA,heightSmall,widthSmall,bpp,2,4);
		for (int i=0; i<6; i++)
		{
			glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, widthSmall, heightSmall, 0, GL_RGB, GL_UNSIGNED_BYTE, sBuffer[i]);
			delete[] sBuffer[i];
		}			 
		delete[] bufferTGA;

			glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
			glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

			// generate the mipmap chain

			glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
			//set the wrapping modes
			glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, filter);
			glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, filter);

	}
	else
	{
		// create the OpenGL ES texture resource and get the handle
		glGenTextures(1, &textureID);
		// bind the texture to the 2D texture type
		glBindTexture(GL_TEXTURE_2D, textureID);

		// create CPU buffer and load it with the image data
		int width, height, bpp;

			char * bufferTGA = LoadTGA(file, &width, &height, &bpp );
			// load the image data into OpenGL ES texture resource

			if(bpp == 24)
			{
				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bufferTGA);
			}
			else
			{
				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bufferTGA);
			}
			// free the client memory

			delete [] bufferTGA;
		//set the filters for minification and magnification

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		// generate the mipmap chain

		glGenerateMipmap(GL_TEXTURE_2D);
		//set the wrapping modes
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, filter);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, filter);
	}
	if (strcmp(file, "") != 0)
	delete[] file;
	return textureID;
}	

Texture::Texture(void)
{
}

Texture::~Texture(void)
{
	glDeleteTextures(1, &textureID);
}
